package org.jsola.hr.web;

import org.jsola.core.Page;
import org.jsola.core.Result;
import org.jsola.exception.ParamException;
import org.jsola.hr.common.HrLogKit;
import org.jsola.hr.constant.HrConstants;
import org.jsola.hr.dto.SalaryChangesAddDTO;
import org.jsola.hr.dto.SalaryChangesUpdateDTO;
import org.jsola.hr.query.SalaryChangesQuery;
import org.jsola.hr.service.ISalaryChangesService;
import org.jsola.hr.vo.SalaryChangesListVO;
import org.jsola.hr.vo.SalaryChangesVO;
import org.jsola.user.core.TokenUser;
import org.jsola.user.core.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;


/**
 * @author zhr
 */
@RestController("hrSalaryChangesController")
@RequestMapping(HrConstants.API_PATH + "/v1/salaryChanges")
public class SalaryChangesController implements ISalaryChangesControllerAPI {

    @Autowired
    private ISalaryChangesService salaryChangesService;

    @Override
    @PostMapping
    public Result<SalaryChangesVO> save(@Valid @RequestBody SalaryChangesAddDTO salaryChangesAddDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 新增员工调薪记录权限校验

        return Result.success(
                salaryChangesService.save(salaryChangesAddDTO, tokenUser)
        );
    }


    @Override
    @PutMapping
    public Result<Integer> update(@Valid @RequestBody SalaryChangesUpdateDTO salaryChangesUpdateDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 修改员工调薪记录权限校验

        int count = salaryChangesService.update(salaryChangesUpdateDTO, tokenUser);
        if (count == 0) {
            throw new ParamException("更新失败，请刷新页面后重试");
        }
        return Result.success(count);
    }



    @Override
    @DeleteMapping(value = "/{salaryChangesId}")
    public Result<Integer> delete(@PathVariable String salaryChangesId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 彻底删除员工调薪记录权限校验

        int count = salaryChangesService.deleteByIds(tokenUser, salaryChangesId);
        if (count == 0) {
            throw new ParamException("删除失败，请刷新页面后重试");
        }
        return Result.success(count);
    }

    @Override
    @DeleteMapping
    public Result<Integer> deleteBatch(@RequestBody String[] salaryChangesIds) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 批量彻底删除员工调薪记录权限校验

        int count = salaryChangesService.deleteByIds(tokenUser, salaryChangesIds);
        if (count == 0) {
            throw new ParamException("删除失败，请刷新页面后重试");
        }
        return Result.success(count);
    }

    @Override
    @GetMapping(value = "/{salaryChangesId}")
    public Result<SalaryChangesVO> findById(@PathVariable String salaryChangesId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看员工调薪记录权限校验

        return Result.success(
            salaryChangesService.selectById(salaryChangesId, tokenUser.getSiteId())
        );
    }

    @Override
    @GetMapping(value = "/listAll")
    public Result<List<SalaryChangesListVO>> listAll(@Valid SalaryChangesQuery salaryChangesQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看员工调薪记录权限校验

        return Result.success(
            salaryChangesService.select(salaryChangesQuery, tokenUser.getSiteId())
        );
    }


    @Override
    @GetMapping
    public Result<Page<SalaryChangesListVO>> page(@Valid SalaryChangesQuery salaryChangesQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看项目权限校验

        return Result.success(
            salaryChangesService.selectPage(salaryChangesQuery, tokenUser.getSiteId())
        );
    }

    @Override
    @GetMapping(value = "/getSalaryBasicByEmpId")
    public Result<SalaryChangesVO> getSalaryBasicByEmpId(String empId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看员工调薪记录权限校验
        HrLogKit.logForSelect("根据员工ID查询基础工资", "EmpInfo", empId, tokenUser);
        return Result.success(
                salaryChangesService.getSalaryBasicByEmpId(empId, tokenUser.getSiteId()).to(SalaryChangesVO.class)
        );
    }


}

